package com.tlkj.common.curd.util;

import com.github.pagehelper.PageInfo;

import java.util.List;

/**
 * @author flyong86
 */
public class PageUtil {
    //手动分页 自定义方法
    public static <T> PageInfo<T> pageInfo(int pageNum, int pageSize, int totalRecord, List<T> list) {
        // 手动封装PageInfo参数进行分页
        PageInfo<T> realPageInfo = new PageInfo<>(list);
        int start = 0;
        int totalPages = 0;
        //设置总数
        realPageInfo.setTotal(totalRecord);
        //设置每页的显示条数
        realPageInfo.setPageSize(pageSize);
        //设置要显示的是第几页的数据
        realPageInfo.setPageNum(pageNum);
        realPageInfo.setSize(totalRecord);
        //计算获取对应的要显示的数据
        if (totalRecord % pageSize == 0) {
            totalPages = totalRecord / pageSize;
        } else {
            totalPages = totalRecord / pageSize + 1;
        }
        realPageInfo.setPages(totalPages);
        //初始边界值计算
        if (pageNum == 1) {
            realPageInfo.setHasPreviousPage(false);
            realPageInfo.setPrePage(0);
            realPageInfo.setIsFirstPage(true);
        } else {
            start = realPageInfo.getPageSize() * (realPageInfo.getPageNum() - 1);
            realPageInfo.setHasPreviousPage(true);
            realPageInfo.setPrePage(pageNum - 1);
            realPageInfo.setIsFirstPage(false);
        }
        realPageInfo.setStartRow((long) (pageNum - 1) * pageSize);
        //结束边界值计算
        if ((start + realPageInfo.getPageSize() > realPageInfo.getTotal())) {
            realPageInfo.setHasNextPage(false);
            realPageInfo.setIsLastPage(true);
            realPageInfo.setEndRow(totalRecord);
        } else {
            realPageInfo.setHasNextPage(true);
            realPageInfo.setNextPage(pageNum + 1);
            realPageInfo.setIsLastPage(false);
            realPageInfo.setEndRow((long) (pageNum) * pageSize);
        }
        if (realPageInfo.getSize() == 0) {
            realPageInfo.setStartRow(0);
            realPageInfo.setEndRow(0);
        } else {
            realPageInfo.setStartRow(realPageInfo.getStartRow() + 1);
            realPageInfo.setEndRow(realPageInfo.getStartRow() - 1 + realPageInfo.getSize());
        }

        realPageInfo.calcByNavigatePages(PageInfo.DEFAULT_NAVIGATE_PAGES);
//        realPageInfo.setNavigateLastPage(totalPages > pageNum ? pageNum + 1 : totalPages);
        return realPageInfo;
    }
}